package com.zhuoyue.biz.core.command.cmd.common.fe6;

import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.biz.core.command.impl.curtains.IntelligentCurtain0Fe60210;
import com.zhuoyue.biz.core.command.model.SilentCurtainCmd;
import com.zhuoyue.common.constant.ControlType;

import java.util.HashMap;
import java.util.Map;

/**
 * 智能窗帘
 * @author Administrator
 */
public class ZhiNengChuangLian0Fe60210 {

    public static String control(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {

        String cmd = "";
        switch (controlReq.getControlType()) {
            //开关
            case ControlType.SWITCH_STATE_REQUEST:
                cmd = getSwitchStopCmd(deviceAndGatewayDO, controlReq);
                break;
            //百分比
            case ControlType.PERCENT_REQUEST:
                cmd = getPercentCmd(deviceAndGatewayDO, controlReq);
                break;
            //状态查询
            case ControlType.CHECK_REQUEST:
                cmd = getCheckCmd(deviceAndGatewayDO);
                break;
            case ControlType.TURN_ON_REQUEST:
                Map<String, Object> control = new HashMap<>(1);
                control.put("state", "OPEN");
                controlReq.setActions(control);
                cmd = getSwitchStopCmd(deviceAndGatewayDO, controlReq);
                break;
            case ControlType.TURN_OFF_REQUEST:
                Map<String, Object> control1 = new HashMap<>(1);
                control1.put("state", "CLOSE");
                controlReq.setActions(control1);
                cmd = getSwitchStopCmd(deviceAndGatewayDO, controlReq);
                break;
            case ControlType.PAUSE_REQUEST:
                Map<String, Object> control2 = new HashMap<>(1);
                control2.put("state", "STOP");
                controlReq.setActions(control2);
                cmd = getSwitchStopCmd(deviceAndGatewayDO, controlReq);
                break;
            default:
                break;
        }


        return cmd;
    }

    /**
     * 状态检查
     *
     * @param deviceAndGatewayDO
     * @return
     */
    private static String getCheckCmd(DeviceAndGatewayDO deviceAndGatewayDO) {

        String gatewayMac = deviceAndGatewayDO.getGatewayMac();
        String deviceMac = deviceAndGatewayDO.getZigbeeMac();

        return new SilentCurtainCmd(gatewayMac, deviceMac, IntelligentCurtain0Fe60210.OD_CHECK).getCmd(IntelligentCurtain0Fe60210.CMD_MODEL);

    }

    /**
     * 开关停
     *
     * @param deviceAndGatewayDO
     * @param controlReq
     * @return
     */
    private static String getSwitchStopCmd(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {

        String gatewayMac = deviceAndGatewayDO.getGatewayMac();
        String deviceMac = deviceAndGatewayDO.getZigbeeMac();
        Map<String, Object> actionsMap = controlReq.getActions();

        String state = actionsMap.get("state").toString();
        
        if (IntelligentCurtain0Fe60210.OPEN.equals(state)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, IntelligentCurtain0Fe60210.OD_OPEN).getCmd(IntelligentCurtain0Fe60210.CMD_MODEL);
        } else if (IntelligentCurtain0Fe60210.CLOSE.equals(state)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, IntelligentCurtain0Fe60210.OD_CLOSE).getCmd(IntelligentCurtain0Fe60210.CMD_MODEL);
        } else if (IntelligentCurtain0Fe60210.STOP.equals(state)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, IntelligentCurtain0Fe60210.OD_STOP).getCmd(IntelligentCurtain0Fe60210.CMD_MODEL);
        }

        return null;
    }


    /**
     * 百分比
     *
     * @param deviceAndGatewayDO
     * @param controlReq
     * @return
     */
    private static String getPercentCmd(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {

        String gatewayMac = deviceAndGatewayDO.getGatewayMac();
        String deviceMac = deviceAndGatewayDO.getZigbeeMac();
        Map<String, Object> actionsMap = controlReq.getActions();

        int value = (int) actionsMap.get("percentage");
        if (IntelligentCurtain0Fe60210.PERCENT_0.equals(value)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, IntelligentCurtain0Fe60210.OD_0).getCmd(IntelligentCurtain0Fe60210.CMD_MODEL);
        } else if (IntelligentCurtain0Fe60210.PERCENT_25.equals(value)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, IntelligentCurtain0Fe60210.OD_25).getCmd(IntelligentCurtain0Fe60210.CMD_MODEL);
        } else if (IntelligentCurtain0Fe60210.PERCENT_50.equals(value)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, IntelligentCurtain0Fe60210.OD_50).getCmd(IntelligentCurtain0Fe60210.CMD_MODEL);
        } else if (IntelligentCurtain0Fe60210.PERCENT_75.equals(value)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, IntelligentCurtain0Fe60210.OD_75).getCmd(IntelligentCurtain0Fe60210.CMD_MODEL);
        } else if (IntelligentCurtain0Fe60210.PERCENT_100.equals(value)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, IntelligentCurtain0Fe60210.OD_100).getCmd(IntelligentCurtain0Fe60210.CMD_MODEL);
        }

        return null;

    }


}
